package com.itheima.app.manager;

import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.mongo.FocusUser;
import com.itheima.service.mongo.FocusUserService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class FocusUserManager {
    @DubboReference
    private FocusUserService focusUserService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //关注
    public void attention(Long focusUserId) {
        Long userId = UserHolder.get().getId();
        //创建关注类
        FocusUser focusUser = new FocusUser();
        //赋值
        focusUser.setCreated(System.currentTimeMillis());
        focusUser.setFocusUserId(focusUserId);
        focusUser.setUserId(userId);
        //调用rpc保存
        focusUserService.saveFocusUser(focusUser);
        //把关注标记存入redis
        stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.FOCUS_USER, userId, focusUserId), "1");
    }

    //取消关注
    public void unAttention(Long focusUserId) {
        Long userId = UserHolder.get().getId();
        //调用rpc取消关注
        focusUserService.removeFocusUser(userId, focusUserId);
        //把redis里关注标记删除
        stringRedisTemplate.delete(StrUtil.format(ConstantUtil.FOCUS_USER, userId, focusUserId));
    }
}
